#ifndef KLOGGER_H__
#define KLOGGER_H__

#include <stdio.h>
#include <stdlib.h>

class KLogger
{
public:
    enum KLoggerLevel{
        LOGGER_LEVEL_DEBUG = 0,
        LOGGER_LEVEL_INFO,
        LOGGER_LEVEL_NOTICE,
        LOGGER_LEVEL_WARN,
        LOGGER_LEVEL_ERROR,
        LOGGER_LEVEL_FATAL
    };
private:
    class KLoggerPrivate;
    KLoggerPrivate *private_ptr;
public:
    static void initGlobal();
    static KLogger* instance();
public:
    KLogger();
    virtual ~KLogger();

    void init(const char*filepath, int rolling_interval, int rolling_size, int rolling_keep_num, const char*output_format);

    void  write(int level, const char*s, size_t len);
    void  print(int level, const char*fmt, ...);

    void  debug(const char*fmt, ...);
    void  info(const char*fmt, ...);
    void  notice(const char*fmt, ...);
    void  warn(const char*fmt, ...);
    void  error(const char*fmt, ...);
    void  fatal(const char*fmt, ...);

    void  write(int level, const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*s, size_t len);
    void  print(int level, const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);

    void  debug(const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);
    void  info(const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);
    void  notice(const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);
    void  warn(const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);
    void  error(const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);
    void  fatal(const char*filename, size_t filename_len, 
                        const char*function, size_t function_len, int line, const char*fmt, ...);    
};
#define klogger_write(log, level, buf, buf_len) \
	(log)->write(level, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	buf, buf_len)
#define klogger_print(log, level, format, args...) \
	(log)->print(level, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)

#define klogger_fatal(log, format, args...) \
	(log)->fatal(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)
#define klogger_error(log, format, args...) \
	(log)->error(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)
#define klogger_warn(log, format, args...) \
	(log)->warn(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)
#define klogger_notice(log, format, args...) \
	(log)->notice(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)
#define klogger_info(log, format, args...) \
	(log)->info(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)
#define klogger_debug(log, format, args...) \
	(log)->debug(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
	format, ##args)


#endif